package com.ingrid.javi.activities;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.ingrid.javi.R;
import com.ingrid.javi.R.id;
import com.ingrid.javi.R.layout;
import com.ingrid.javi.R.string;
import com.ingrid.javi.activities.validation.FormFilledListener;
import com.ingrid.javi.model.DAO;
import com.ingrid.javi.model.EditTextValidator;
import com.ingrid.javi.model.Movie;
import com.ingrid.javi.model.NonEmptyTextValidator;
import com.ingrid.javi.model.ValueInRangeValidator;
import com.ingrid.javi.util.AlertController;

public class RegistryActivity extends Activity {

	private EditText etTitulo;
	private EditText etAno;
	private EditText etSinopse;
	private EditText etNota;
	private int registryMode;
	private LinearLayout llRatingContainer;

	public static final String EXTRA_MODE = "extraMode";
	public static final int EXTRA_MODE_REGISTRY_MOVIE = 1;
	public static final int EXTRA_MODE_WISH_LIST = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.registry_movie);

		this.registryMode = this.getIntent().getIntExtra(EXTRA_MODE,
				EXTRA_MODE_REGISTRY_MOVIE);

		this.etTitulo = (EditText) findViewById(R.id.etTitulo);
		this.etAno = (EditText) findViewById(R.id.etAno);
		this.etSinopse = (EditText) findViewById(R.id.etSinopse);
		this.etNota = (EditText) findViewById(R.id.etNota);
		this.llRatingContainer = (LinearLayout) findViewById(R.id.llRatingContainer);
		final Button btCadastrar = (Button) findViewById(R.id.btCadastrar);

		btCadastrar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (registryMode == EXTRA_MODE_REGISTRY_MOVIE) {
					onClickRegistryMovie();
				} else {
					onClickRegistryWish();
				}
			}

		});

		if (this.registryMode == EXTRA_MODE_REGISTRY_MOVIE) {
			btCadastrar.setText("Cadastrar");

			new FormFilledListener(new EditTextValidator[] {
					new NonEmptyTextValidator(this.etTitulo),
					new ValueInRangeValidator(this.etAno, 1900,
							Integer.MAX_VALUE),
					new NonEmptyTextValidator(this.etSinopse),
					new ValueInRangeValidator(this.etNota, 0, 10) },
					btCadastrar);
		} else {
			btCadastrar.setText("Cadastrar Desejo");
			llRatingContainer.setVisibility(View.GONE);

			new FormFilledListener(new EditTextValidator[] {
					new NonEmptyTextValidator(this.etTitulo),
					new ValueInRangeValidator(this.etAno, 1900,
							Integer.MAX_VALUE),
					new NonEmptyTextValidator(this.etSinopse) }, btCadastrar);
		}
	}

	private void onClickRegistryMovie() {
		String titulo = etTitulo.getText().toString();
		String ano = etAno.getText().toString();
		String sinopse = etSinopse.getText().toString();
		String nota = etNota.getText().toString();
		Movie movie = new Movie(titulo, ano, sinopse, nota);

		this.saveMovie(movie);
	}

	private void saveMovie(Movie movie) {
		new DAO(this).save(movie);
		AlertController.showSuccessMessage(this,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						RegistryActivity.this.finish();
					}
				}, this.getString(R.string.registry_movie));
	}

	private void onClickRegistryWish() {
		String titulo = etTitulo.getText().toString();
		String ano = etAno.getText().toString();
		String sinopse = etSinopse.getText().toString();
		Movie movie = new Movie(titulo, ano, sinopse);

		this.saveWish(movie);
	}

	private void saveWish(Movie movie) {
		new DAO(this).save(movie);
		AlertController.showSuccessMessage(this,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						RegistryActivity.this.finish();
					}
				}, this.getString(R.string.registry_movie));
	}
}
